package com.seeyon.apps.nygj.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class HtKit {
    public static String getHtCode(String formType, Long memberId, String formCode, String guid, String ywlx, String orgCode) throws Exception {
        /*获取token*/
        String base64Str = SystemKit.RES_APP + ":" + SystemKit.key;
        String Authorization = StrKit.stringToBase64(base64Str);
        Map<String, Object> headerMap = new HashMap<String, Object>();
        headerMap.put("appID", SystemKit.RES_APP);
        headerMap.put("Authorization", "Basic " + Authorization);
        headerMap.put("appIDnew", SystemKit.appIDnew);
        headerMap.put("keySecretnew", SystemKit.keySecretnew);
        String result = HttpKit.get(SystemKit.tokenUrl, headerMap);
        JSONObject tokenObj = JSONObject.parseObject(result);
        String code_token = StrKit.str(tokenObj.get("code"));
        String F_HTBH = "";
        if ("00000".equals(code_token)) {
            String token = StrKit.str(tokenObj.getJSONObject("data").get("token"));
            Map<String, Object> dataHeaderMap = new HashMap<String, Object>();
            dataHeaderMap.put("appID", SystemKit.RES_APP);
            dataHeaderMap.put("Authorization", "Basic " + Authorization);
            dataHeaderMap.put("auth-token", token);
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("F_YWLX", ywlx);
            paramMap.put("GUID", guid);
            paramMap.put("RES_APP", SystemKit.appId);
            paramMap.put("RES_ORG", orgCode);
            String dataJson = JSONObject.toJSONString(paramMap);
            String url = "";
            if ("ht".equals(formType)) {
                url = SystemKit.getAgreementNumberUrl;
            } else if ("jsd".equals(formType)) {
                url = SystemKit.voucherNumberUrl;
            }
            if (!"".equals(url)) {
                String dataResult = HttpKit.post(url, dataHeaderMap, dataJson);
                String msg = "";
                if ("jsd".equals(formType)) {
                    msg = "获取财务一体化凭证编号接口";
                } else {
                    msg = "获取财务一体化合同编号接口";
                }
                FormCap4Kit.addLogs(memberId, msg, formCode, "JKCGBS_S", dataJson, dataResult);
                JSONObject resultObj = JSONObject.parseObject(dataResult);
                JSONObject dataObj = resultObj.getJSONObject("data");
                String code = StrKit.str(dataObj.get("code"));
                if ("0".equals(code)) {
                    JSONObject dataJsonObject = dataObj.getJSONObject("data");
                    if (dataJsonObject.containsKey("rowSetArray")) {
                        JSONArray rowSetArray = dataJsonObject.getJSONArray("rowSetArray");
                        for (int i = 0; i < rowSetArray.size(); i++) {
                            JSONObject rowSetObj = rowSetArray.getJSONObject(i);
                            JSONObject dataMapObj = rowSetObj.getJSONObject("dataMap");
                            if (dataMapObj.containsKey("F_HTBH")) {
                                F_HTBH = StrKit.str(dataMapObj.get("F_HTBH"));
                            } else if (dataMapObj.containsKey("SAP_BELNR")) {
                                F_HTBH = StrKit.str(dataMapObj.get("SAP_BELNR"));
                            }

                        }
                    }
                }
            }
        }
        return F_HTBH;
    }
}
